import simplejson as json
import urllib
import re 

from Acquisition import aq_inner
from Products.Five.browser import BrowserView
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile

from portalh8.services import config

class BuscaGengiscanView(BrowserView):
    
    __call__ = ViewPageTemplateFile('busca_gengiscan.pt')
    
    def buscar_rede(self):
        query = self.context.REQUEST.get('q','')
        if not query:
            return []
        
        gengiscan_site = urllib.urlopen("http://sereia/scan/service/search/"+query.replace(' ','%20'))
        corpo_site = gengiscan_site.read()
        
        resultados = json.loads(corpo_site)
        
        return resultados['result']
    
    def permissao_ver(self):
        ip = self.context.REQUEST['REMOTE_ADDR']
        return ('Authenticated' in self.context.portal_membership.getAuthenticatedMember().getRoles()) and (re.match(r'^161\.24\.7[2-5]\.\d{1,3}$',ip) or ip=='127.0.0.1')